/* ********************************
 * 
 * author:  sunsc
 * date:    06/25/2013
 * update:  06/25/2013
 * file: riskComm.h 
 * brief: Copyright (c) 2013 Sand
 * 
 ********************************/
#ifndef  __H_RISK_COMM
#define	 __H_RISK_COMM
 
#include "head.h"
#include "riskConst.h"
#include "hiredis.h"
#include "cJSON.h"
#include "tcp.h"
#include "inifile.h"
#include "zlog.h"
#include "databaseOper.h"
#include <sqlite3.h>

//////////// datastruct define district /////////////////

//for log
zlog_category_t *zcGlobalLog;
//for parent contact to child each other
int fdParent[2], fdChild[2];
//save child process work state
typedef struct {
    pid_t pid;
    char status; 
}pid_state_st; 
//business rules datastruct
typedef struct{
	unsigned int nChargeAmt;
	unsigned int nDeductAmt;
	unsigned int nGapTime;
	unsigned int nProcessTime;	
}busi_rules_st;

//for globe setting
busi_rules_st	 sGlobeBusinessRules;

//for sqlite3 database 
sqlite3	*globeDb;

//for globe Date
char szglobeDate[9];
//for change database
unsigned int  nglobeOutputDataBaseFlag;
unsigned int  nglobeCurrentDataBaseFlag;

//trans Data
typedef struct{
	char szcardNo[17];
	char szbeginTime[15];
	char szendTime[15];
	char sztransAmt[13];
	char szendAmt[13];
	char sztransType[5];
	char sztransName[64];
	char sztransMid[16];
	char sztransTid[9];
	char sztransRetCode[7];
	char sztransResult[64];
	char sztransMessage[8192];	
}trans_data_st;





//////////// function define district /////////////////
 
//init log system
int 
init_log(const char *szfileName);

//for only one proccess running
int 
write_pid();

//for backend run
void daemon_init();

//for business process
void 
fork_process(int nlistenId);

int
child_process(int nsockfd);

//get rules context
int
get_busi_rules();

//oprator redis database
int
fn_storeMonitorData(cJSON	*iTransData,char *szRecvTransData);

int
fn_redisStore2Args(redisContext *idb,char *szoper, char *szkey, char *szcontent);

int
fn_redisStore3Args(redisContext *idb,char *szoper, char *szkey, char *szfeild, char *szcontent);

int
add_processResult(redisContext *idb,trans_data_st *stransData);

int
add_processFail(redisContext *idb,trans_data_st *stransData);

int
add_processOverTime(redisContext *idb,trans_data_st *stransData);

int
add_processReport(redisContext *idb,trans_data_st *stransData);

int
analyse_transData();

int
get_localHostTime(char *pszTime);

int 
mktm(char *isztime,struct tm *otm);

int
get_diffTime(char *ibeginTime, char *iendTime,double *odiffTime);

int
change_DataBaseIndex();

//rules table just has only one record
int
get_rulesFromSqlite(void *NotUsed, int colNum, char **colVal, char **szColName);


 
 
 
 
#endif

